﻿using SigowayCommonLib.ADO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using WarehouseManager.Common;

namespace WarehouseManager.BL
{
    internal class BillCreateBL
    {
        private DBManager dbManager;

        public BillCreateBL ()
        {
            this.dbManager = DBManager.GetDBManager();
        }

        internal DataTable GetPatientInfo(string key, PatientSearchEnum searchEnum)
        {
            string strWhereFormate = "WHERE {0} LIKE ? ";
            IDictionary<string, object> dicParameter = new Dictionary<string, object>();
            string strWhere = string.Empty;
            switch (searchEnum)
            {
                //姓名
                case PatientSearchEnum.Name:
                    strWhere = string.Format(strWhereFormate, ConstInfo.COL_PATIENT_NAME);
                    dicParameter.Add(ConstInfo.COL_PATIENT_NAME, "%" + key + "%");
                    break;
                //拼音
                case PatientSearchEnum.Pinyin:
                    strWhere = string.Format(strWhereFormate, ConstInfo.COL_PYCODE);
                    dicParameter.Add(ConstInfo.COL_PYCODE, "%" + key + "%");
                    break;
                //编码
                case PatientSearchEnum.Num:
                    strWhere = string.Format(strWhereFormate, ConstInfo.COL_NUMBER);
                    dicParameter.Add(ConstInfo.COL_NUMBER, "%" + key + "%");
                    break;
                default:
                    return null;
            }

            var sqlText = "SELECT * FROM PatientInfo " + strWhere;
            return dbManager.ExecuteQuery(new SQLObject(sqlText, dicParameter));         
        }

        internal DataTable GetDrugInfo(string drugSpec)
        {
            var sqlText = string.Format("SELECT * FROM DrugInfo WHERE {0} LIKE '%{1}%' escape '/'", ConstInfo.COL_DRUGSPEC, 
                (drugSpec.IndexOf('%') != -1) ? drugSpec.Replace("%","/%") : drugSpec);

            return dbManager.ExecuteQuery(new SQLObject(sqlText, null));
        }

        /// <summary>
        ///  当前日期的最大番号取得
        /// </summary>
        /// <param name="kpdate"></param>
        /// <returns></returns>
        internal long GetMaxBileNo(string kpdate)
        {
            string sqlText = string.Format("SELECT Max({0}) FROM BileHistory WHERE {1} = '{2}' ", ConstInfo.COL_BILLNO, ConstInfo.COL_KPDATE, kpdate);

            object objBileNo = this.dbManager.GetSingleValue(new SQLObject(sqlText, null));
            long lBileNo;
            if (objBileNo != null && !string.IsNullOrEmpty(objBileNo.ToString()) && long.TryParse(objBileNo.ToString(), out lBileNo))
            {
                return lBileNo;
            }

            return -1;
        }

        internal bool RegisterBileHistoryInfo(DataTable datSource,List<string> lstBillNoGUI,string operterID,string strDateNow)
        {
            if (datSource == null || datSource.Rows.Count == 0)
            {
                return false;
            }

            //更新数据
            DataTable datUpdate = datSource.Clone();
            //插入数据
            DataTable datInsert = datSource.Clone();
            foreach (DataRow row in datSource.Rows)
            {
                //画面的单据号
                string strBillNoGUI = row[ConstInfo.COL_BILLNO_GUI].ToString();

                //画面的单据号包含
                if (lstBillNoGUI != null && lstBillNoGUI.Contains(strBillNoGUI))
                {
                    DataRow rowUpdate = datUpdate.NewRow();
                    rowUpdate.ItemArray = row.ItemArray;
                    datUpdate.Rows.Add(rowUpdate);
                }
                else
                {
                    DataRow rowInsert = datInsert.NewRow();
                    rowInsert.ItemArray = row.ItemArray;
                    datInsert.Rows.Add(rowInsert);
                }
            }
            datUpdate.AcceptChanges();
            datInsert.AcceptChanges();

            //插入
            if (datInsert.Rows.Count <= 0)
            {
                return true;
            }
            string sqlText =
                    string.Format(
                        "INSERT INTO BileHistory({0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13}) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                        ConstInfo.COL_KPDATE, ConstInfo.COL_BILLNO, ConstInfo.COL_ARTNO, ConstInfo.COL_DRUGNAME,
                        ConstInfo.COL_DRUGSPEC, ConstInfo.COL_UNIT, ConstInfo.COL_PRODUCEAREA, ConstInfo.COL_DRUGCOUNT,
                        ConstInfo.COL_PATIENT_NAME, ConstInfo.COL_MOBILENO, ConstInfo.COL_ADDR, ConstInfo.COL_NOTE,
                        ConstInfo.COL_CREATEUSER, ConstInfo.COL_CREATEDATE);

            List<IDictionary<string, object>> parameterList = new List<IDictionary<string, object>>();
            foreach (DataRow row in datInsert.Rows)
            {
                IDictionary<string, object> dicParameter = new Dictionary<string, object>();
                dicParameter.Add(ConstInfo.COL_KPDATE, row[ConstInfo.COL_KPDATE]);
                dicParameter.Add(ConstInfo.COL_BILLNO, row[ConstInfo.COL_BILLNO]);
                dicParameter.Add(ConstInfo.COL_ARTNO, row[ConstInfo.COL_ARTNO]);
                dicParameter.Add(ConstInfo.COL_DRUGNAME, row[ConstInfo.COL_DRUGNAME]);
                dicParameter.Add(ConstInfo.COL_DRUGSPEC, row[ConstInfo.COL_DRUGSPEC]);
                dicParameter.Add(ConstInfo.COL_UNIT, row[ConstInfo.COL_UNIT]);
                dicParameter.Add(ConstInfo.COL_PRODUCEAREA, row[ConstInfo.COL_PRODUCEAREA]);
                dicParameter.Add(ConstInfo.COL_DRUGCOUNT, row[ConstInfo.COL_DRUGCOUNT]);
                dicParameter.Add(ConstInfo.COL_PATIENT_NAME, row[ConstInfo.COL_PATIENT_NAME]);
                dicParameter.Add(ConstInfo.COL_MOBILENO, row[ConstInfo.COL_MOBILENO]);
                dicParameter.Add(ConstInfo.COL_ADDR, row[ConstInfo.COL_ADDR]);
                dicParameter.Add(ConstInfo.COL_NOTE, row[ConstInfo.COL_NOTE]);
                dicParameter.Add(ConstInfo.COL_CREATEUSER, operterID);
                dicParameter.Add(ConstInfo.COL_CREATEDATE, strDateNow);

                parameterList.Add(dicParameter);
            }

            dbManager.ExecuteNonQuery(new SQLObject(sqlText, null, parameterList));
            return true;
        }

        #region CreateDataSource
        /// <summary>
        ///  一览数据的数据源
        /// </summary>
        /// <returns></returns>
        internal DataTable CreateDataSource()
        {
            DataTable dtSource = new DataTable();
            //单据号画面表示
            dtSource.Columns.Add(ConstInfo.COL_BILLNO_GUI, typeof(string));
            //货号
            dtSource.Columns.Add(ConstInfo.COL_ARTNO, typeof(string));
            //品名
            dtSource.Columns.Add(ConstInfo.COL_DRUGNAME, typeof(string));
            //规格
            dtSource.Columns.Add(ConstInfo.COL_DRUGSPEC, typeof(string));
            //单位
            dtSource.Columns.Add(ConstInfo.COL_UNIT, typeof(string));
            //产地
            dtSource.Columns.Add(ConstInfo.COL_PRODUCEAREA, typeof(string));
            //数量
            dtSource.Columns.Add(ConstInfo.COL_DRUGCOUNT, typeof(string));
            //患者姓名
            dtSource.Columns.Add(ConstInfo.COL_PATIENT_NAME, typeof(string));
            //患者住址
            dtSource.Columns.Add(ConstInfo.COL_ADDR, typeof(string));
            //备注
            dtSource.Columns.Add(ConstInfo.COL_NOTE, typeof(string));
            //单据号
            dtSource.Columns.Add(ConstInfo.COL_BILLNO, typeof(int));
            //开票日期
            dtSource.Columns.Add(ConstInfo.COL_KPDATE, typeof(string));
            //电话
            dtSource.Columns.Add(ConstInfo.COL_MOBILENO, typeof(string));
            dtSource.AcceptChanges();
            return dtSource;
        }
        #endregion

        internal DataTable getBillList(DateTime dateFrom, DateTime dateTo)
        {
            string sqlText = string.Format("SELECT * FROM BileHistory WHERE {0} Between '{1}' and '{2}'", 
                                            ConstInfo.COL_KPDATE, DateTimeUtil.GetDateString(dateFrom), DateTimeUtil.GetDateString(dateTo));

            return this.dbManager.ExecuteQuery(new SQLObject(sqlText, null));
        }
    }
}
